package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.mapper.QuestionMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import javax.swing.*;
import java.util.Date;
import java.util.List;

@Service
public class RecommendUserApiImpl implements RecommendUserApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public RecommendUser findTodayBest(Long userId) {
        Query query = new Query();
        Criteria criteria = new Criteria().where("toUserId").is(userId)
                .and("date").is(new Date());
        query.with(Sort.by(Sort.Order.desc("score")));
        query.addCriteria(criteria);
        query.limit(1);
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }

    @Override
    public PageResult<RecommendUser> findRecommendation(Long userId, Integer page, Integer pagesize) {
        Query query = new Query();
        query.addCriteria(Criteria.where("toUserId").is(userId));
        query.with(Sort.by(Sort.Order.desc("score")));
        /*
        按照缘分值排序查推荐列表
         */
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
        //查总数量
        long count = mongoTemplate.count(query, RecommendUser.class);
        //封装到PageResult里
        PageResult<RecommendUser> recommendUserPageResult = new PageResult<>();
        int pages = (int) Math.ceil(count*0.1/pagesize);
        recommendUserPageResult.setPage(page);
        recommendUserPageResult.setPagesize(pagesize);
        recommendUserPageResult.setCounts((int) count);
        recommendUserPageResult.setPages(pages);
        return recommendUserPageResult;
    }

    @Override
    public RecommendUser personalInfo(Long targetUserId, Long userId) {
        Query query = new Query();

        query.addCriteria(Criteria.where("userId").is(targetUserId).and("toUserId").is(userId));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }




}
